/**
 * Created by kevin on 2017/7/20.
 */

var JPush = require("jpush-sdk");

var client = JPush.buildClient('c3c6921b221d5a67d93603e1', '9e821b884dce8696e64d2e4a');

var push = {};

push.pushIOS = function (message,json,cb) {
    client.push().setPlatform('ios')
        .setAudience(JPush.ALL)
        .setOptions(null,3600*24,null,true)
        .setNotification(message, JPush.ios(message, 'happy', 1,false,json))
        .send(function(err, res) {
            if (err) {
                if (cb) {
                    cb(err,null);
                }
                console.log(err.message);
            } else {
                if (cb) {
                    cb(null,{
                        sendNo : res.sendno,
                        msgId : res.msg_id
                    })
                }
                console.log({
                    sendNo : res.sendno,
                    msgId : res.msg_id
                });
            }
        });
};

push.pushAndroid = function (title,message,json,cb) {
    client.push().setPlatform('android')
        .setAudience(JPush.ALL)
        .setNotification(message, JPush.android(message, title, 1,json))
        .send(function(err, res) {
            if (err) {
                if (cb) {
                    cb(err,null);
                }
                console.log(err.message);
            } else {
                if (cb) {
                    cb(null,{
                        sendNo : res.sendno,
                        msgId : res.msg_id
                    })
                }
                console.log({
                    sendNo : res.sendno,
                    msgId : res.msg_id
                });
            }
        });
};

module.exports = push;

